package com.zhx.threadcoreknowledge.stopthread;

/**
 * @author ：zhanghaixuan
 * @date ：Created in 2019-09-29 09:23
 * 用途         ： 如果while 循环里面放try/catch,会导致线程中断失效 // 因为 catch住了  但是线程代码没有执行完毕
 * sleep 响应了 interrupt 后  会把 interrupt 状态清除
 */
public class CantInterrupt {

    public static void main(String[] args) throws InterruptedException {
        Runnable runnable = () ->{

                int num = 0;

                while (num <= 10000
                       && !Thread.currentThread().isInterrupted()
                ){
                    if(num % 100 == 0){
                        System.out.println(num +"可以被100整除");
                    }
                    num++;
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("结束");

        };

        Thread thread = new Thread(runnable);
        thread.start();
        Thread.sleep(5000);
        thread.interrupt();
    }
}
